<template>
    <div class="edit-popup">
        <popup
            ref="popupRef"
            :title="popupTitle"
            :async="true"
            width="550px"
            @confirm="handleSubmit"
            @close="handleClose"
        >
            <el-form
                ref="formRef"
                :model="formData"
                :rules="formRules"
                label-width="100px"
                class="mt-4"
            >
                <company-select ref="companySelectRef" @select-company="handleSelectCompany" custom-class="w-[380px]" />
                <el-form-item label="姓名" prop="name">
                    <el-input v-model="formData.name" class="w-[380px]" placeholder="请输入姓名" />
                </el-form-item>
                <el-form-item label="手机号" prop="mobile">
                    <el-input v-model="formData.mobile" class="w-[380px]" placeholder="请输入手机号" />
                </el-form-item>
                <el-form-item label="身份证" prop="license">
                    <el-input v-model="formData.license" class="w-[380px]" placeholder="请输入身份证号" />
                </el-form-item>
                <el-form-item label="注册地编码" prop="region_code">
                    <el-input v-model="formData.region_code" class="w-[380px]" placeholder="请输入注册地编码" />
                </el-form-item>
                <el-form-item label="车牌号" prop="vehicle_no">
                    <el-input v-model="formData.vehicle_no" class="w-[380px]" placeholder="请输入车牌号" />
                </el-form-item>
            </el-form>
        </popup>
    </div>
</template>

<script lang="ts" setup>
import type { FormInstance } from 'element-plus'
import Popup from '@/components/popup/index.vue'
import feedback from '@/utils/feedback'
import CompanySelect from '@/components/company-select/index.vue'
import { driverSave } from '@/api/supervise/driver'

const emit = defineEmits(['success', 'close'])
const formRef = shallowRef<FormInstance>()
const popupRef = shallowRef<InstanceType<typeof Popup>>()
const companySelectRef = shallowRef<InstanceType<typeof CompanySelect>>()
    console.log(companySelectRef.value)
const mode = ref('add')
const popupTitle = computed(() => {
    return mode.value === 'add' ? '新增司机' : '编辑司机'
})

const formData = reactive({
    company_id: '',
    name: '',
    mobile: '',
    license: '',
    region_code: '',
    vehicle_no: ''
})

const formRules = {
    company_id: [{ required: true, message: '请选择所属公司' }],
    name: [{ required: true, message: '请输入姓名' }],
    mobile: [{ required: true, message: '请输入手机号' }],
    license: [{ required: true, message: '请输入身份证号' }],
    region_code: [{ required: true, message: '请输入注册地编码' }],
    vehicle_no: [{ required: true, message: '请输入车牌号' }]
}

const handleSubmit = async () => {
    await formRef.value?.validate()
    try {
        const data = { ...formData }
        await driverSave(data)
        feedback.msgSuccess(mode.value === 'add' ? '新增成功' : '编辑成功')
        emit('success')
        handleClose()
    } catch (error) {
        console.error(error)
    }
}

const handleClose = () => {
    emit('close')
}

const open = (type = 'add') => {
    mode.value = type
    popupRef.value?.open()
}

const getDetail = async (data: Record<string, any>) => {
    Object.assign(formData, data)
    await nextTick()
    companySelectRef.value?.setCompanyId(data.company_id)
    formData.vehicle_no = data.car.vehicle_no
}

const handleSelectCompany = (val: any) => {
    formData.company_id = val
}

defineExpose({
    open,
    getDetail
})
</script>